package com.road7.mina.customCodec;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.road7.mina.simpledemo.PBMessage;
import com.road7.mina.simpledemo.TimeClientHandler;

/**
 * @Description :
 * @FileName: Client.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年11月20日 上午10:58:55
 * @Version:V1.00
 */
public class Client {

	public static void main(String[] args) {
		IoConnector connector = new NioSocketConnector(Runtime.getRuntime().availableProcessors() + 1);
		connector.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); // 设置编码过滤器
		connector.setConnectTimeoutMillis(30000);
		connector.setHandler(new TimeClientHandler());// 设置事件处理器
		ConnectFuture cf = connector.connect(new InetSocketAddress("127.0.0.1", 9123));// 建立连接
		cf.awaitUninterruptibly();// 等待连接创建完成
		PBMessage message = new PBMessage((short) 1000);
		message.setEmpty1(5);
		message.setEmpty1(10);
		message.setPlayerId(20);
		message.setMessage(null);
		cf.getSession().write(message);// 发送消息
		message.setCode((short) 1500);
		cf.getSession().write(message);// 发送消息
		message.setCode((short) 2000);
		cf.getSession().write(message);// 发送消息
		message.setCode((short) 2500);
		cf.getSession().write(message);// 发送消息
		cf.getSession().getCloseFuture().awaitUninterruptibly();// 等待连接断开
		connector.dispose();
	}
}
